package orz.itrip.biz.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import orz.itrip.DtoUtils;
import orz.itrip.biz.service.HotelRoomService;
import orz.itrip.biz.vo.GetRoomVo;
import orz.itrip.dto.Dto;
import orz.itrip.mapper.*;
import orz.itrip.pojo.HotelRoom;
import orz.itrip.pojo.Image;
import orz.itrip.pojo.LabelDic;
import orz.itrip.vo.GetPreOrderVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class HotelRoomServiceImpl implements HotelRoomService {

    @Resource
    private LabelDicMapper labelDicMapper;
    @Resource
    private HotelMapper hotelMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;
    @Resource
    private HotelOrderMapper hotelOrderMapper;
    @Override
    public Dto getimg(Long hotelId) {
        List<Image> images = hotelMapper.selectImage(hotelId,1);
        if (images!=null){
            return DtoUtils.getSuccesWithData(images,"获取酒店图片房型成功");
        }
        return DtoUtils.getFalseDto("获取酒店图片房型失败","40001");
    }

    @Override
    public Dto queryhotelroombed() {
        LabelDic labelDic = new LabelDic();
        labelDic.setParentId(1L);
        List<LabelDic> labelDics = labelDicMapper.select(labelDic);
        if (labelDics!=null){
            return DtoUtils.getSuccesWithData(labelDics,"获取成功");
        }
        return DtoUtils.getFalseDto("获取失败","40002");
    }

    @Override
    public Dto queryhotelroombyhotel(GetRoomVo getRoomVo) {
        if (getRoomVo!=null){
            GetPreOrderVo preOrderVo = new GetPreOrderVo();
            preOrderVo.setCheckInDate(getRoomVo.getStartDate());
            preOrderVo.setCheckOutDate(getRoomVo.getEndDate());
            preOrderVo.setHotelId(getRoomVo.getHotelId());
            HotelRoom hotelRoom = new HotelRoom();
            BeanUtils.copyProperties(getRoomVo,hotelRoom);
            List<HotelRoom> hotelRooms = hotelRoomMapper.select(hotelRoom);
            List list = new ArrayList();
            for (HotelRoom hr:hotelRooms) {
                preOrderVo.setRoomId(hr.getId());
                Integer store = hotelOrderMapper.selectStore(preOrderVo);
                if (store==null){
                    store = hotelOrderMapper.selectProduct(preOrderVo);
                }
                Integer orderScore = hotelOrderMapper.selectOrderScore(preOrderVo);
                if (orderScore==null){
                    orderScore = 0;
                }
                if (store-orderScore>0) {
                    HotelRoom[] hotelRooms1 = {hr};
                    list.add(hotelRooms1);
                }
            }
            return DtoUtils.getSuccesWithData(list,"获取成功");
        }
        return DtoUtils.getFalseDto("获取失败","40003");
    }
}
